package org.vsg.security.model.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.vsg.common.biz.exception.ServiceException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.carrier.Carrier;
import org.vsg.common.pagination.carrier.RecordLimitCarrier;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.common.pagination.datatrans.PaginationProviderFactory;
import org.vsg.security.model.domain.SysRole;
import org.vsg.security.model.domain.SysUser;
import org.vsg.security.model.persistence.SysRoleDao;
import org.vsg.security.model.persistence.SysUserDao;
import org.vsg.security.model.service.SysUserService;


public class SysUserServiceImpl implements SysUserService {
	/**
	 * set the pagination provider factory
	 * 
	 * 
	 */
	private PaginationProviderFactory factory = PaginationProviderFactory.getInstance();

	private SysUserDao sysUserDao;
	
	@Autowired
	private SysRoleDao sysRoleDao;
	
	

	/**
	 * @param carrier
	 * @return
	 * @exception ServiceException
	 * @exception PaginationException
	 */
	public Pagination pageSysUser(Carrier carrier) throws ServiceException,
			PaginationException {
		// TODO: implement
		PaginationProvider provider = factory
				.generateProvider((RecordLimitCarrier) carrier);
		return sysUserDao.pageSysUser(provider);
	}

	/**
	 * @param carrier
	 * @return
	 * @throws ServiceException
	 * @exception PaginationException
	 */
	public Pagination pageSysUserByAll(Carrier carrier) throws ServiceException,
			PaginationException {
		// TODO: implement
		PaginationProvider provider = factory
				.generateProvider((RecordLimitCarrier) carrier);
		return sysUserDao.pageSysUserByAll(provider);
	}
	
	@Override
	public void saveAll(Collection<SysUser> all)
			throws ServiceException {
		// TODO Auto-generated method stub
		sysUserDao.saveAll(all);
		
	}  	

	/**
	 * @param sysUser
	 * @exception ServiceException
	 */
	public void addSysUser(SysUser sysUser) throws ServiceException {
		// TODO: implement
		sysUserDao.save(sysUser);
	}

	/**
	 * @param sysUser
	 * @exception ServiceException
	 */
	public void updateSysUser(SysUser sysUser) throws ServiceException {
		// TODO: implement
		sysUserDao.update(sysUser);
	}

	/**
	 * @param sysUser
	 * @exception ServiceException
	 */
	public void updateSimpleSysUser(SysUser newEntity) throws ServiceException {
		// TODO: implement
		SysUser original = sysUserDao.load( newEntity.getId() );
		
		original.setCode( newEntity.getCode() );
		original.setName( newEntity.getName() );
		original.setDescription( newEntity.getDescription() );
		original.setFlagDisable( newEntity.getFlagDisable() );
		original.setPassword( newEntity.getPassword() );
	
		// --- update the method ---
		sysUserDao.update( original );		
		
	}

	
	
	@Override
	public void removeUsers(int[] ids) throws ServiceException {
		// TODO Auto-generated method stub
		Collection<SysUser> users = new Vector<SysUser>();
		SysUser user = null;
		for (int i = 0 ; i < ids.length ; i++) {
			user =  this.sysUserDao.load( ids[i]);
			user.setFlagDeleted( (short)1 );
			users.add( user );
		}
		this.sysUserDao.saveAll( users );		
	}

	/**
	 * @param sysUser
	 * @exception ServiceException
	 */
	public void deleteSysUser(SysUser sysUser) throws ServiceException {
		// TODO: implement
		sysUserDao.delete(sysUser);
	}

	@Override
	public void deleteAll(Collection<SysUser> all)
			throws ServiceException {
		// TODO Auto-generated method stub
		sysUserDao.deleteAll(all);
	}
	

	/**
	 * @param pk
	 * @exception ServiceException
	 */
	public SysUser loadSysUser(Serializable pk) throws ServiceException {
		// TODO: implement
		return sysUserDao.load(pk);
	}


	@Override
	public Collection<SysUser> findByAll() throws ServiceException {
		// TODO Auto-generated method stub
		return sysUserDao.findByAll();
	}


	public SysUserDao getSysUserDao() {
		return sysUserDao;
	}

	/**
	 * @param newSysUserDao
	 */
	public void setSysUserDao(SysUserDao newSysUserDao) {
		sysUserDao = newSysUserDao;
	}

	
	
	public SysRoleDao getSysRoleDao() {
		return sysRoleDao;
	}

	public void setSysRoleDao(SysRoleDao sysRoleDao) {
		this.sysRoleDao = sysRoleDao;
	}

	@Override
	public void assignRolesToUser(SysUser user, String[] roleIds)
			throws ServiceException {
		// TODO Auto-generated method stub
		SysUser original = sysUserDao.load( user.getId() );
		
		Set<SysRole> assignedRole = original.getAssignedRoles();
		
		SysRole role = null;
		for (int i = 0 ; i < roleIds.length ; i++) {
			role = sysRoleDao.load( new Integer(roleIds[i]) );
			assignedRole.add( role );
		}
		
		sysUserDao.update( original );
	}

	@Override
	public void removeUserAssignedRole(SysUser user, String[] roleIds)
			throws ServiceException {
		// TODO Auto-generated method stub
		SysUser original = sysUserDao.load( user.getId() );
		
		Set<SysRole> assignedRole = original.getAssignedRoles();
		
		SysRole role = null;
		for (int i = 0 ; i < roleIds.length ; i++) {
			role = sysRoleDao.load( new Integer(roleIds[i]) );
			assignedRole.remove(role);
		}
		
		sysUserDao.update( original );
		
	}




}